package com.xmy.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String driver = null;
    private static String url = null;
    private static String user = null;
    private static String pwd = null;
    Connection conn = null;

    static {
        init();
    }

    /**
     * 读取文件属性
     */
    public static void init(){
        Properties properties = new Properties();
        try{
            InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
            properties.load(is);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            pwd = properties.getProperty("pwd");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     */
    public Connection getConnection(){
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,user,pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 执行增删改
     * @return
     */
    public int executeUpdate(String sql,Object[] objects){
        PreparedStatement pstmt = null;
        Connection conn = null;
        int count = 0;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            for (int i = 0;i<objects.length;i++){
                pstmt.setObject(i+1,objects[i]);
            }
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 关闭连接
     * @param rs
     * @param pstmt
     * @param conn
     */
    public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){
        try{
            if(rs!=null){
                rs.close();
            }
            if(pstmt!=null){
                pstmt.close();
            }
            if (conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询
     * @param sql
     * @param objects
     * @return
     */
    public ResultSet executeQuery(String sql, Object[] objects){
        PreparedStatement pstmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try{
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            if(objects!=null) {
                for (int i = 0; i < objects.length; i++) {
                    pstmt.setObject((i + 1), objects[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
